﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VerticalWebSpider
{
    public class StringList
    {
        public const string NEW_PATH = "NewPath";

        public const string HISTORY_PATH = "HistoryPath";
        
        public const string DOWNLOAD_FILE_PATH = "DownLoadFilePath";

        private StringList() { }

        private List<string> _StringList = new List<string>();

        private static Dictionary<string, StringList> _Name2Instance = new Dictionary<string, StringList>();

        public static StringList GetInstance(string name)
        {
            if (!_Name2Instance.ContainsKey(name))
            {
                lock (_Name2Instance)
                {
                    if (!_Name2Instance.ContainsKey(name))
                    {
                        _Name2Instance[name] = new StringList();
                    }
                }
            }

            return _Name2Instance[name];
        }

        public string Pop()
        {
            lock (_StringList)
            {
                var first = _StringList.FirstOrDefault();
                if (_StringList.Count > 0)
                    _StringList.RemoveAt(0);
                return first;
            }
        }

        public string Peek()
        {
            lock (_StringList)
                return _StringList.FirstOrDefault();
        }

        public void Add(string path)
        {
            lock (_StringList)
            {
                if (!_StringList.Contains(path))
                    _StringList.Add(path);
            }
        }

        public void Remove(string path)
        {
            lock (_StringList)
                _StringList.Remove(path);
        }

        public bool IsPassed(string path)
        {
            lock (_StringList)
                return _StringList.Contains(path);
        }
    }
}
